﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace is_it_messy
{


    class autoSortFiles
    {
        // 单位KB
        public static readonly long[] sizeSortArr = { 10, 100, 1024, 16384 };

        //
        public static readonly string[] pictureType = {
            "jp2", "jpeg", "jpg", "ico", "gif", "icns", "dds"
            //"bmp","pbm", "cur", "mng", "pgm", "png", "tif", "tiff", "wbmp",
            //"webp", "xbm", "xpm", "psd", "svg", "svgz", "tga", "psb", "ppm"
        };
        public static readonly string[] mediaType = {
            "mp3", "mp4"
        };
        public static readonly string[] documentType = {
            "doc","docx", "ppt","pdf"
        };
        public static readonly string[] compressType = {
            "7z", "rar", "tgz"
        };
        public static readonly string[][] types = {
            mediaType, pictureType, documentType, compressType
        };
        public static List<string[]> typeSortArr = new List<string[]>(types);


        public static List<List<FileData>> autoSort(string standar, string path, bool recursion=true)
        {
            List<FileData> datas = Util.GetFilesInfo(path, recursion);

            return autoSort(standar, datas);
        }

        public static List<List<FileData>> autoSort(string standar, List<FileData> datas)
        {

            if (standar.Equals("size"))
            {
                return sortBySize(datas);
            }
            else if (standar.Equals("type"))
            {
                return sortByType(datas);
            }
            else if (standar.Equals("type"))
            {
                return sortByType(datas);
            }
            else if(standar.Equals("modifyTime"))
            {

                return sortByModifyTime(datas);
            }
            return null;
        }

        private static List<List<FileData>> sortBySize(List<FileData> files)
        {
            List<List<FileData>> result = new List<List<FileData>>();

            List<FileData> level1 = new List<FileData>();
            List<FileData> level2 = new List<FileData>();
            List<FileData> level3 = new List<FileData>();
            List<FileData> level4 = new List<FileData>();
            List<FileData> level5 = new List<FileData>();

            result.Add(level1);
            result.Add(level2);
            result.Add(level3);
            result.Add(level4);
            result.Add(level5);

            foreach (FileData file in files)
            {
                long size = file.Size;
                for (int i = 0; i < result.Count - 1; i++)
                {
                    long standarSize = sizeSortArr[i] * 1024;
                    if (size < standarSize)
                    {
                        result[i].Add(file);
                        break;
                    }
                    if (i == result.Count - 2)
                    {
                        result[i + 1].Add(file);
                    }
                }
            }
            return result;
        }

        private static List<List<FileData>> sortByType(List<FileData> files)
        {
            // init lists
            List<List<FileData>> result = new List<List<FileData>>();
            List<FileData> meidaFiles = new List<FileData>();
            List<FileData> pictureFiles = new List<FileData>();
            List<FileData> documentFiles = new List<FileData>();
            List<FileData> compressFiles = new List<FileData>();
            result.Add(meidaFiles);
            result.Add(pictureFiles);
            result.Add(documentFiles);
            result.Add(compressFiles);
            List<FileData> unknowFiles = new List<FileData>();
            result.Add(unknowFiles);

            foreach (FileData file in files)
            {
                string fileType = file.Type;
                Boolean sorted = false;
                int length = fileType.Length - 1;

                if(length>0)
                {
                    fileType = fileType.Substring(1, length);
    
                    for (int i = 0; i < typeSortArr.Count; i++)
                    {
                        string[] typeList = typeSortArr[i];
                        if (typeList.ToList().IndexOf(fileType) != -1)
                        {
                            result[i].Add(file);
                            sorted = true;
                            break;
                        }
                    }
                }
                if (!sorted)
                {
                    result.Last().Add(file);
                }
            }
            return result;
        }

        private static List<List<FileData>> sortByModifyTime(List<FileData> files)
        { 

            // init list
            List<List<FileData>> result = new List<List<FileData>>();

            List<FileData> todayFiles = new List<FileData>();
            List<FileData> yesterdayFiles = new List<FileData>();
            List<FileData> thisWeekFiles = new List<FileData>();
            List<FileData> lastWeekFiles = new List<FileData>();
            List<FileData> thisMonthFiles = new List<FileData>();
            List<FileData> thisYearFiles = new List<FileData>();
            List<FileData> farFiles = new List<FileData>();

            List<FileData>[] lists = { todayFiles, yesterdayFiles, thisWeekFiles, thisMonthFiles, thisYearFiles, farFiles };
            result.AddRange(lists);
            
            DateTime dt = DateTime.Today;
            DateTime yesterday = dt.AddDays(-1);

            DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); //本周周一
            DateTime lastStartWeek = startWeek.AddDays(-7); //上周周一
            //DateTime endWeek = startWeek.AddDays(6); //本周周日
            DateTime startMonth = dt.AddDays(1 - dt.Day); //本月月初
            //DateTime endMonth = startMonth.AddMonths(1).AddDays(-1); //本月月末
            //DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day); //本季度初
            //DateTime endQuarter = startQuarter.AddMonths(3).AddDays(-1); //本季度末
            DateTime startYear = new DateTime(dt.Year, 1, 1); //本年年初
            //DateTime endYear = new DateTime(dt.Year, 12, 31); //本年年末


            foreach (FileData file in files)
            {
                DateTime modifyTime = file.LastWriteTime;

                if(modifyTime.CompareTo(dt)>0)
                {
                    todayFiles.Add(file);
                }
                else if(modifyTime.CompareTo(yesterday)>0)
                {
                    yesterdayFiles.Add(file);
                }
                else if (modifyTime.CompareTo(startWeek) > 0)
                {
                    thisWeekFiles.Add(file);
                }
                else if (modifyTime.CompareTo(lastStartWeek) > 0)
                {
                    lastWeekFiles.Add(file);
                }
                else if (modifyTime.CompareTo(startMonth) > 0)
                {
                    thisMonthFiles.Add(file);
                }
                else if (modifyTime.CompareTo(startYear) > 0)
                {
                    thisYearFiles.Add(file);
                }
                else
                {
                    farFiles.Add(file);
                }
            } 
            return result;
        }



    }
}

